import { defineStore } from 'pinia';
import { ref, type Ref } from 'vue';

export const useKeepAliveStore = defineStore('KeepAlive', () => {
  const firstKeepAliveRoute: Ref<string[]> = ref([]);
  const secondKeepAliveRoute: Ref<string[]> = ref([]);

  function setKeepAlive(keepAliveRoute: string[], name: string) {
    if (!keepAliveRoute.includes(name)) {
      keepAliveRoute.push(name);
    }
  }

  function setNoKeepAlive(keepAliveRoute: string[], name: string) {
    if (keepAliveRoute.includes(name)) {
      const index = keepAliveRoute.findIndex((item) => {
        return item === name;
      });
      keepAliveRoute.splice(index, 1);
    }
  }

  return {
    firstKeepAliveRoute,
    secondKeepAliveRoute,
    setKeepAlive,
    setNoKeepAlive
  };
});
